ActiveReports 11
Custom Web Exporting
ActiveReports 11 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Page Report/RDL Report Walkthroughs > Export > Custom Web Exporting

ActiveReports provides components that allow you to export your reports into several popular formats like PDF, HTML, Excel, Image, Word and XML.

The walkthrough is split up into the following activities:

Note: Although this walkthrough uses Page reports, you can also implement this using RDL reports.

To add an ActiveReport to the Visual Studio project

  1. Create a new ASP.NET Web Application project.
  2. From the Project menu, select Add New Item.
  3. In the Add New Item dialog that appears, select ActiveReports 11 Page Report and in the Name field, rename the file as CustomWebExporting.
  4. Click the Add button to open a new fixed page report in the designer.
  5. In the Solution Explorer, right-click the References node and select Add Reference.
  6. In the Add Reference dialog that appears, select the following references and click OK to add them to your project.
    GrapeCity.ActiveReports.Export.Pdf.v11
    GrapeCity.ActiveReports.Export.Html.v11
    GrapeCity.ActiveReports.Export.Excel.v11
    GrapeCity.ActiveReports.Export.Word.v11
    GrapeCity.ActiveReports.Export.Image.v11
    GrapeCity.ActiveReports.Export.Xml.v11

See Adding an ActiveReport to a Project for information on adding different report layouts.

To add code to the Web Form to create the PDF Export object and export a report

  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
'Provide the page report you want to render.
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

'Set the rendering extension and render the report.
Dim pdfRenderingExtension As New GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()
reportDocument.Render(pdfRenderingExtension, outputProvider)

Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline;filename=MyExport.pdf")
Dim ms As New System.IO.MemoryStream()
CType(outputProvider.GetPrimaryStream().OpenStream(), System.IO.MemoryStream).WriteTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.End()             

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension pdfRenderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
reportDocument.Render(pdfRenderingExtension, outputProvider);

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=MyExport.pdf");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();        

Note: To use the one-touch printing option, add the following to the code above.

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Replace the line reportDocument.Render(pdfRenderingExtension, outputProvider) in the code above with the following

Dim setting As New GrapeCity.ActiveReports.Export.Pdf.Page.Settings()
setting.PrintOnOpen = True
reportDocument.Render(pdfRenderingExtension, outputProvider,setting)
C# code. Paste INSIDE the Page Load event.
Copy Code
// Replace the line reportDocument.Render(pdfRenderingExtension, outputProvider); in the code above with the following

GrapeCity.ActiveReports.Export.Pdf.Page.Settings setting = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();
setting.PrintOnOpen = true;
reportDocument.Render(pdfRenderingExtension, outputProvider,setting);
Warning: You need to manually license your application in order to use PDF export.

To add code to the Web Form to create the HTML Export object and export a report

  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Provide the page report you want to render.
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

' Set the rendering extension and render the report.
Dim htmlRenderingExtension As New GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()
Dim setting As New GrapeCity.ActiveReports.Export.Html.Page.Settings()
setting.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Galley
setting.MhtOutput = True

reportDocument.Render(htmlRenderingExtension, outputProvider, setting)

Response.ContentType = "message/rfc822"
Response.AddHeader("content-disposition", "inline;filename=MyExport.mht")
Dim ms As New System.IO.MemoryStream()
CType(outputProvider.GetPrimaryStream().OpenStream(), System.IO.MemoryStream).WriteTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.End()             

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension htmlRenderingExtension = new GrapeCity.ActiveReports.Export.Html.Page.HtmlRenderingExtension();GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();GrapeCity.ActiveReports.Export.Html.Page.Settings setting = new GrapeCity.ActiveReports.Export.Html.Page.Settings();
setting.Mode = GrapeCity.ActiveReports.Export.Html.Page.RenderMode.Galley;
setting.MhtOutput = true;

reportDocument.Render(htmlRenderingExtension, outputProvider, setting);           

Response.ContentType = "message/rfc822";
Response.AddHeader("content-disposition", "inline;filename=MyExport.html");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();           

To add code to the Web Form to create the Excel Export

       
  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Provide the page report you want to render.
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

' Provide settings for your rendering output.
Dim excelSetting As New GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings()
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx
Dim setting As GrapeCity.ActiveReports.Extensibility.Rendering.ISettings = excelSetting

' Set the rendering extension and render the report.
Dim excelRenderingExtension As New GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()
reportDocument.Render(excelRenderingExtension, outputProvider, setting.GetSettings())

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "inline;filename=MyExport.xls")
Dim ms As New System.IO.MemoryStream()
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.[End]()       

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Provide settings for your rendering output.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx;
GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
reportDocument.Render(excelRenderingExtension, outputProvider, setting.GetSettings());

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "inline;filename=MyExport.xls");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();                    

To add code to the Web Form to create the Word Export object and export a report

  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.
    Note: To export your report in .Doc format, change the FileFormat property option from .Docx to .Doc format as depicted below. 
    wordSetting.FileFormat = GrapeCity.ActiveReports.Export.Word.Page.FileFormat.Doc

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Provide the page report you want to render
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

' Set the rendering extension and render the report
Dim wordRenderingExtension As New GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()

' Set FileFormat property to .Docx
Dim wordSetting As New GrapeCity.ActiveReports.Export.Word.Page.Settings()
wordSetting.FileFormat = GrapeCity.ActiveReports.Export.Word.Page.FileFormat.OOXML

reportDocument.Render(wordRenderingExtension, outputProvider, wordSetting)
Response.ContentType = "application/msword"
Response.AddHeader("content-disposition", "inline;filename=MyExport.docx")
Dim ms As New System.IO.MemoryStream()
CType(outputProvider.GetPrimaryStream().OpenStream(), System.IO.MemoryStream).WriteTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.End()

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Set the rendering extension and render the report
GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension wordRenderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();

// Set the FileFormat property to .Docx
GrapeCity.ActiveReports.Export.Word.Page.Settings wordSetting = new GrapeCity.ActiveReports.Export.Word.Page.Settings();
wordSetting.FileFormat = GrapeCity.ActiveReports.Export.Word.Page.FileFormat.OOXML;

reportDocument.Render(wordRenderingExtension, outputProvider, wordSetting);
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", "inline;filename=MyExport.docx");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();

To add code to the Web Form to create the Image Export object and export a report

  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Provide the page report you want to render.
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

' Set the rendering extension and render the report.
Dim imageRenderingExtension As New GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()
Dim setting As New GrapeCity.ActiveReports.Export.Image.Page.Settings()
setting.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.JPEG
reportDocument.Render(imageRenderingExtension, outputProvider, setting)

Response.ContentType = "image/jpeg"
Response.AddHeader("content-disposition", "inline;filename=MyExport.jpg")
Dim ms As New System.IO.MemoryStream()

' Get the first page of the report
CType(outputProvider.GetSecondaryStreams()(0).OpenStream(), System.IO.MemoryStream).WriteTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.End()                     

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension imageRenderingExtension = new GrapeCity.ActiveReports.Export.Image.Page.ImageRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
GrapeCity.ActiveReports.Export.Image.Page.Settings setting = new GrapeCity.ActiveReports.Export.Image.Page.Settings();
setting.ImageType = GrapeCity.ActiveReports.Export.Image.Page.Renderers.ImageType.JPEG;      

reportDocument.Render(imageRenderingExtension, outputProvider, setting);

Response.ContentType = "image/jpeg";
Response.AddHeader("content-disposition", "inline;filename=MyExport.jpg");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// Get the first page of the report
outputProvider.GetSecondaryStreams()[0].OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();                            

To add code to the Web Form to create the XML Export object and export a report.

  1. Double-click on the design view of the aspx page. This creates an event-handling method for the Page_Load event.
  2. Add code like the following to the Page_Load event.

To write the code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Page Load event.
Copy Code
' Provide the page report you want to render.
Dim report As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo(Server.MapPath("") + "\CustomWebExporting.rdlx"))
Dim reportDocument As New GrapeCity.ActiveReports.Document.PageDocument(report)

' Set the rendering extension and render the report.
Dim xmlRenderingExtension As New GrapeCity.ActiveReports.Export.Xml.Page.XmlRenderingExtension()
Dim outputProvider As New GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider()
reportDocument.Render(xmlRenderingExtension, outputProvider)

Response.ContentType = "application/xml"
Response.AddHeader("content-disposition", "inline;filename=MyExport.xml")
Dim ms As New System.IO.MemoryStream()
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms)
Response.BinaryWrite(ms.ToArray())
Response.[End]()                          

To write the code in C#

C# code. Paste INSIDE the Page Load event.
Copy Code
// Provide the page report you want to render.
GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("") + "\\CustomWebExporting.rdlx"));
GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

// Set the rendering extension and render the report.
GrapeCity.ActiveReports.Export.Xml.Page.XmlRenderingExtension xmlRenderingExtension = new GrapeCity.ActiveReports.Export.Xml.Page.XmlRenderingExtension();
GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
reportDocument.Render(xmlRenderingExtension, outputProvider);

Response.ContentType = "application/xml";
Response.AddHeader("content-disposition", "inline;filename=MyExport.xml");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
Response.BinaryWrite(ms.ToArray());
Response.End();                        

To run the project

Press F5 to run the project.

See Also